package jbstoremgmt.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.FetchMode;
import org.hibernate.criterion.Restrictions; //import org.springframework.orm.hibernate3.HibernateTemplate; 
import jbstoremgmt.domain.BPMaster;

public class BPDAOImpl implements BPDAO {

	// private HibernateTemplate hibernateTemplate;
	SessionFactory sessionfactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionfactory = sessionFactory;
	}

	public int deleteBP(int id) {
		// TODO Auto-generated method stub
		int resultRows =0;
		String delBpMaster = "delete from BPMaster as bpmaster "
				+ "where bpmaster.bp_id=:id";
 
		try 
		{
			this.sessionfactory.getCurrentSession().getTransaction().begin();

			Query query = this.sessionfactory.getCurrentSession().createQuery(
					delBpMaster).setInteger("id", id);
			resultRows = query.executeUpdate();
			
			this.sessionfactory.getCurrentSession().getTransaction().commit();
			
			this.sessionfactory.getCurrentSession().close();
			this.sessionfactory.getCurrentSession().disconnect();
			
			
		} catch (Exception e) {
			System.out.println("Exception Occured while executing deletion");
			e.printStackTrace();
			resultRows =-1;
			
		}
		return resultRows;

	}

	public BPMaster getBP(int id) {
		// TODO Auto-generated method stub
		String qryBpMaster = "from BPMaster as bpmaster "
				+ "where bpmaster.bp_id=:id";
		this.sessionfactory.getCurrentSession().beginTransaction();

		List<BPMaster> bpMasterList = this.sessionfactory.getCurrentSession()
				.createQuery(qryBpMaster).setInteger("id", id).list();

		this.sessionfactory.getCurrentSession().close();
		this.sessionfactory.getCurrentSession().disconnect();
		return bpMasterList.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<BPMaster> getBPAll() {
		this.sessionfactory.getCurrentSession().getTransaction().begin();
		List bpMasterList = this.sessionfactory.getCurrentSession()
				.createQuery("from BPMaster").list();
		this.sessionfactory.getCurrentSession().getTransaction().commit();
		this.sessionfactory.getCurrentSession().close();

		return bpMasterList;
	}

	public void modifyBP(BPMaster bpMaster) {
		this.sessionfactory.getCurrentSession().beginTransaction();
		this.sessionfactory.getCurrentSession().update(bpMaster);
		this.sessionfactory.getCurrentSession().getTransaction().commit();
		this.sessionfactory.getCurrentSession().close();
		System.out.println("Object Modifie--> "+bpMaster.getBp_id());
		
	}

	public void saveBP(BPMaster bpMaster) {

		System.out.println("id is : " + bpMaster.getBp_id());

		this.sessionfactory.getCurrentSession().beginTransaction();
		this.sessionfactory.getCurrentSession().saveOrUpdate(bpMaster);
		this.sessionfactory.getCurrentSession().getTransaction().commit();
		this.sessionfactory.getCurrentSession().close();
		System.out.println("Object Inserted. "+bpMaster.getBp_id());
	}

}